package binary_tree.leetcode.easy;

/**
 * @author bruin_du
 * @description 二叉树的直径
 * @date 2022/9/18 23:56
 **/
public class Num543_DiameterOfBinaryTree {
    int res = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) return 0;
        dfs(root);
        return res;
    }

    int dfs(TreeNode root) {
        if (root == null) return 0;
        int left = dfs(root.left);
        int right = dfs(root.right);
        res = Math.max(res, left + right);
        return Math.max(left, right) + 1;
    }
}
